﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZhangZhao.Common;

namespace ZhangZhao.WebAPI
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string result = Output.Fail;

            try
            {
                string UserName = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.UserName]);
                string Password = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.Password]);

                if (!string.IsNullOrWhiteSpace(UserName) && !string.IsNullOrWhiteSpace(Password))
                {
                    ZhangZhao.BLL.H_User bll = new ZhangZhao.BLL.H_User();

                    if (PageValidate.IsEmail(UserName) && !bll.EmailExists(UserName))
                    {
                        result = Output.UserNameNotExist;
                    }
                    else if (!bll.Exists(UserName))
                    {
                        result = Output.UserNameNotExist;
                    }
                    else
                    {
                        ZhangZhao.Model.H_User LoginUser = bll.GetModel(UserName, Password);

                        if (LoginUser == null)
                        {
                            result = Output.PasswordInvalid;
                        }
                        else if (LoginUser.IsEffective)
                        {
                            result = Output.Success; //登陆成功
                        }
                        else
                        {
                            result = Output.Fail; //用户已失效，登陆失败。
                        }
                    }
                }
            }
            catch (Exception ex){
                result = Output.Exception + ":" + ex.Message; //网络系统异常，登陆失败
            }

            Response.Clear();
            Response.Write(result);
            Response.End();
        }
    }
}